
// NilKeyGeneratorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NilKeyGenerator.h"
#include "NilKeyGeneratorDlg.h"
#include "NilKeyPlus.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CNilKeyGeneratorDlg dialog




CNilKeyGeneratorDlg::CNilKeyGeneratorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNilKeyGeneratorDlg::IDD, pParent)
	, m_nNormalClusterNumber(1800)
	, m_nNormalClusterSize(100)
	, m_nNormalClusterRadius(64)
	, m_nRangeClusterNumber(20)
	, m_nRangeClusterSize(1000)
	, m_nRangeSearchStep(6)
	, m_nMinRangeClusterRadius(16)
	, m_nMaxRangeClusterRadius(96)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CNilKeyGeneratorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT6, m_nNormalClusterNumber);
	DDX_Text(pDX, IDC_EDIT8, m_nNormalClusterSize);
	DDX_Text(pDX, IDC_EDIT7, m_nNormalClusterRadius);
	DDX_Text(pDX, IDC_EDIT9, m_nRangeClusterNumber);
	DDX_Text(pDX, IDC_EDIT13, m_nRangeClusterSize);
	DDX_Text(pDX, IDC_EDIT12, m_nRangeSearchStep);
	DDX_Text(pDX, IDC_EDIT10, m_nMinRangeClusterRadius);
	DDX_Text(pDX, IDC_EDIT11, m_nMaxRangeClusterRadius);
}

BEGIN_MESSAGE_MAP(CNilKeyGeneratorDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_GENERATE, &CNilKeyGeneratorDlg::OnBnClickedButtonGenerate)
END_MESSAGE_MAP()


// CNilKeyGeneratorDlg message handlers

BOOL CNilKeyGeneratorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	// TODO: Add extra initialization here
	srand(GetTickCount());
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNilKeyGeneratorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CNilKeyGeneratorDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNilKeyGeneratorDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CNilKeyGeneratorDlg::OnBnClickedButtonGenerate()
{
	// TODO: Add your control notification handler code here
	CFileDialog file(FALSE,"","Data.nil",6,"Nilsimsa Key Files (*.nil)|*.nil||");
	if(file.DoModal()!=IDOK)
	{
		return;
	}
	this->UpdateData(TRUE);
	CString & strDataPath = file.GetPathName();
	CString strNormalSeedPath(strDataPath.Left(strDataPath.GetLength()-file.GetFileName().GetLength()));
	CString strRangeSeedPath(strNormalSeedPath);
	strNormalSeedPath.Append("Normal_Seed.seed");
	strRangeSeedPath.Append("Range_Seed.seed");
	ofstream fout_data(strDataPath.GetString());
	ofstream fout_seed1(strNormalSeedPath.GetString());
	ofstream fout_seed2(strRangeSeedPath.GetString());
	if(fout_data&&fout_seed1&&fout_seed2)
	{
		vector<CNilKeyPlus> cNorMalSeeds;
		for(unsigned i = 0;i<m_nNormalClusterNumber;i++)
		{
			cNorMalSeeds.push_back(CNilKeyPlus());
		}
		vector<CNilKeyPlus> cRangeSeeds;
		for(unsigned i = 0;i<m_nRangeClusterNumber;i++)
		{
			cRangeSeeds.push_back(CNilKeyPlus());
		};
		vector<CNilKeyPlus> cTotal;
		for(unsigned int i = 0;i<m_nNormalClusterNumber;i++)
		{
			vector<CNilKeyPlus> cNormalCluster(m_nNormalClusterSize,cNorMalSeeds[i]);
			for(unsigned int j = 0;j<m_nNormalClusterSize;j++)
			{
				if(j!=0)
				{
					cNormalCluster[j].InverseBits(rand_range(0,m_nNormalClusterRadius));
				}
				fout_data<<cNormalCluster[j].ToString()<<endl;
			}
			cTotal.insert(cTotal.end(),cNormalCluster.begin(),cNormalCluster.end());
		}
		for(unsigned int i = 0;i<m_nRangeClusterNumber;i++)
		{
			vector<CNilKeyPlus> cRangeCluster(m_nRangeClusterSize,cRangeSeeds[i]);
			for(unsigned j = 0;j<m_nRangeClusterSize;j++)
			{
				if(j!=0)
				{
					cRangeCluster[j].InverseBits(rand_range(0,m_nMaxRangeClusterRadius));
				}
				fout_data<<cRangeCluster[j].ToString()<<endl;
			}
			cTotal.insert(cTotal.end(),cRangeCluster.begin(),cRangeCluster.end());
		}
		for(unsigned int i = 0;i<m_nNormalClusterNumber;i++)
		{
			unsigned nRes = 0;
			CNilKeyPlus & Seed = cNorMalSeeds[i];
			for(unsigned j =0;j<cTotal.size();j++)
			{
				if(CNilKey::GetDistance(Seed,cTotal[j])<=(int)m_nNormalClusterRadius)
				{
					nRes++;
				}
			}
			fout_seed1<<Seed.ToString()<<endl<<nRes<<endl;
		}

		for(unsigned int i = 0;i<m_nRangeClusterNumber;i++)
		{
			CNilKeyPlus & Seed = cRangeSeeds[i];
			fout_seed2<<Seed.ToString()<<endl;
			for(unsigned nStep = 0;nStep<m_nRangeSearchStep;nStep++)
			{
				int nCurThreshold =(int) m_nMinRangeClusterRadius+ nStep*(m_nMaxRangeClusterRadius-m_nMinRangeClusterRadius)/(m_nRangeSearchStep-1);
				unsigned nRes = 0;
				for(unsigned j =0;j<cTotal.size();j++)
				{
					if(CNilKey::GetDistance(Seed,cTotal[j])<=nCurThreshold)
					{
						nRes++;
					}
				}
				fout_seed2<<nRes<<endl;
			}
		}
		this->MessageBox("Success!");
	}
	else
	{
		this->MessageBox("Create File Error");
	}
	
}
